<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" class="mt10">
      <div slot="header" class="f16">
        <span style="font-weight: 600">材料订购明细</span>
      </div>
      <Table
        :tableHeader="tableHeader"
        :tableData="tableData"
        class="mt10"
        :summaryData="summaryData"
        summary
        :isLoading="isLoading"
        @selectList="selectList1"
        :getTableList="getTableList"
        @confirmChange="confirmChange"
        @currentRow="clickRow"
      />
    </el-card>
    <el-card shadow="never" :body-style="{ padding: '5px' }">
      <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'MaterialRrdering'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import { selectList, selectSomeList } from '@/plugins/mixin'
import LoginWork from '@/utils/LoginWork'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'
import Api from '@/api/GeneralManager/inquire/OrderSummary'

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '品牌',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商代码 厂商名称',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入订购单号',
    inputValue: ''
  },

  {
    type: 'date',
    dateLabel: '订购日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])

const tableHeader = ref<any>([
  {
    change: true,
    label: '类型',
    prop: 'type'
  },
  {
    change: true,
    label: '指令',
    prop: 'lotNo',
    width: '160px'
  },
  {
    change: true,
    label: '型体',
    prop: 'style'
  },
  {
    change: true,
    label: '品牌',
    prop: 'brand'
  },
  {
    change: true,
    label: '颜色',
    prop: 'color'
  },
  {
    change: true,
    label: '订单数',
    prop: 'orderQty',
    width: 50
  },
  {
    label: '号码',
    prop: 'sizeNo',
    width: 50
  },
  {
    change: true,
    label: '订购单号',
    prop: 'purNo',
    width: 130
  },
  {
    change: true,
    label: '订购日期',
    prop: 'purDate',
    width: 150
  },
  {
    change: true,
    label: '请购数量',
    prop: 'purQty'
  },
  {
    change: true,
    label: '物料编码',
    prop: 'mtlNo',
    width: 100
  },
  {
    change: true,
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    change: true,
    label: '材料名称',
    prop: 'mtlName',
    width: 140
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    width: 50
  },
  {
    change: true,
    label: '单位',
    prop: 'mtlUnit',
    width: 50
  },
  {
    change: true,
    label: '厂商',
    prop: 'vendorName'
  },
  {
    change: true,
    label: '交期',
    prop: 'deliveryDate'
  },
  {
    label: '备注',
    prop: 'remark',
    width: 150
  },
  {
    label: '订单备注信息',
    prop: 'orderRemark',
    width: 150
  },
  {
    label: '责任人',
    prop: 'dutyUser',
    width: 150
  },
  {
    label: '责任原因',
    prop: 'dutyRek',
    width: 200
  }
])

// 表一模块
const currentClickRow = ref()
const clickRow = (row: any = tableData.value[0]) => {
  currentClickRow.value = JSON.parse(JSON.stringify(row))
}
// 置空按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/pcOrder/downloadMtlPurList`, {
      choose: '0',
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令号'
      )?.inputValue,
      style: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '型体')
        ?.selectValue,
      moldNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
        ?.selectValue,
      vendorName: formList.value.find(
        (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
      )?.selectValue,
      purNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入订购单号'
      )?.inputValue,
      purEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
      )?.dateValue?.[1],
      purStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
      )?.dateValue?.[0],
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 5,
    num: 0
  },
  {
    index: 9,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/pcOrder/mtlPurList', {
    choose: '0',
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令号'
    )?.inputValue,
    style: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '型体')
      ?.selectValue,
    moldNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    vendorName: formList.value.find(
      (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
    )?.selectValue,
    purNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入订购单号'
    )?.inputValue,
    purEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
    )?.dateValue?.[1],
    purStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
    )?.dateValue?.[0],
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  // currentRow()
  summaryData.value[0].num = tableDataList.qtyTotal
  summaryData.value[1].num = tableDataList.purQtyTotal
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const selectedLists = ref([])
const selectList1 = (selectedList: any) => {
  selectedLists.value = selectedList
}
// 责任原因,责任人 临时保存变量
const dutyReks = ref([])
const dutyUseres = ref([])
const getSelectList = async () => {
  await selectList(formList, Api.getSelectList)
  let vendorName = await service.get<any, any>('/back/vendorQuery')
  let { dutyRek, dutyUsers } = await service.get<any, any>('/back/duty/getDutyCondition')
  dutyReks.value = dutyRek
  dutyUseres.value = dutyUsers
  // let { molds } = await service.get<any, any>('/back/order/allOrderCondition')
  let vendorNames = formList.value.find(
    (item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
  ) as any
  // let mold = formList.value.find((item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '底模号') as any
  // // 插入底模
  // if (molds && mold) {
  //   (molds as string[]).forEach((item: string, index) => {
  //     if (item === null) return
  //     mold.selectOptions?.push({
  //       optionKey: index,
  //       optionValue: item,
  //       optionLabel: item
  //     })
  //   })
  // }
  // 插入厂商代码 厂商名称
  if (vendorName && vendorNames) {
    vendorName.forEach((item: any, index: any) => {
      if (item === null) return
      vendorNames.selectOptions?.push({
        optionKey: index,
        optionValue: `${item.vendorNo} ${item.vendorName}`
      })
    })
  }
}
onMounted(() => {
  getSelectList()
})
// 修改
const confirmChange = async (row: any) => {
  console.log(row)
  row.dutyRek = row.dutyRek.selectValue || ''
  row.dutyUser = row.dutyUser.selectValue || ''
  let res = await LoginWork.post<any, any>('/material/createPcOrderMtlOrder', {
    createFlag: 3,
    list: [row]
  })
  if (res.code === 200) {
    JMessage.success(res.message)
  } else {
    JMessage.warning(res.message)
  }
}
</script>

<style lang="less" scoped>
:deep(.el-form-item:nth-last-child(5)) {
  .el-select {
    width: 200px !important;
    height: 28px;
  }
}

// 主体样式
.ProductionDynamicMain {
  width: 100%;
  height: 670px;
  margin-top: 20px;

  .ProductionDynamicMainCard {
    width: 100%;
    height: 750px;
    box-shadow: none;
  }
}

.ProductionDynamicMainCardMain {
  width: 100%;
  height: 600px;
  display: flex;
  margin-left: 20px;
}

.ProductionDynamicMainCardBottom {
  width: 98%;
  height: 110px;
  margin: auto;
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}

.MainLeftCardH1 {
  font-size: 16px;
  margin: 10px 0 10px 0;
}

.ProductionDynamicMainCardBottomBtn {
  margin-top: 30px;
}

:deep(.elForm .el-form-item__label) {
  color: red !important;
}

.ProductionDynamicMainCardMainLeft {
  width: 100%;
}
</style>
